package com.btl.cygf;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONObject;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.yzx.yunyanzhengdemo.activity.LoginActivity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class YiJianActivity extends Activity implements OnClickListener{

	ImageView login_backtofragment;
	TextView yijiantijiao;
    EditText yijian;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.yijian);
		login_backtofragment = (ImageView) findViewById(R.id.login_backtofragment);
		yijiantijiao=(TextView) findViewById(R.id.yijiantijiao);
		yijian=(EditText) findViewById(R.id.yijian);
		login_backtofragment.setOnClickListener(this);
		yijiantijiao.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.login_backtofragment:
			this.finish();
			break;
			
		case R.id.yijiantijiao:
			
				yiJianTiJiao();
				this.finish();
				Toast.makeText(getApplicationContext(), "提交成功", 1).show();
				break;
			}
			
		}
	private void yiJianTiJiao() {
		if(yijian.getText().toString().equals("")) {
			Toast.makeText(getApplicationContext(), "内容不能为空", 1).show();
			
		} else {
			AsyncHttpClient client = new AsyncHttpClient();
			RequestParams params = new RequestParams();
			
			params.add("yijian", yijian.getText().toString());
			
			SharedPreferences share = getSharedPreferences("login",
					Activity.MODE_WORLD_READABLE);

			String id = share.getString("id", "");
			params.add("id", id);
			client.post(getResources().getString(R.string.url) + "/CYGF/YiJianTiJiaoOp", params,
					new JsonHttpResponseHandler() {
				// 链接成功做出的反应
				@Override
				public void onSuccess(int statusCode, Header[] headers,
						JSONArray response) {
					JSONObject object1 = response.optJSONObject(1);
					JSONObject object = response.optJSONObject(0);
					String result = object.optString("msg");
					Toast.makeText(YiJianActivity.this, result, 1).show();
					super.onSuccess(statusCode, headers, response);
					
					Intent it = new Intent();
					it.setClass(YiJianActivity.this, MainActivity.class);
					it.putExtra("type", 1);
					it.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					startActivity(it);
				}

				// 链接网络失败，做出的反应
				@Override
				public void onFailure(int statusCode, Header[] headers,
						Throwable throwable, JSONArray errorResponse) {
					super.onFailure(statusCode, headers, throwable,
							errorResponse);
				}
			});
		}
		
	}

}
